package com.masterclient.app.modules.income.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.widget.RelativeLayout.ALIGN_PARENT_LEFT;
import static android.widget.RelativeLayout.ALIGN_PARENT_RIGHT;

/**
 * @auther hjian
 * @date 2020/6/16.
 */
public class ProfitLineRatiosView extends LinearLayout {

    @BindView(R.id.profit_lineratio_ratio)
    TextView profitLineratioRatio;
    @BindView(R.id.profit_lineratio_bubble)
    View profitLineratioBubble;
    @BindView(R.id.profit_lineratio_bubblelayout)
    RelativeLayout profitLineratioBubblelayout;
    @BindView(R.id.profit_lineratio_left)
    View profitLineratioLeft;
    @BindView(R.id.profit_lineratio_right)
    View profitLineratioRight;
    @BindView(R.id.profit_lineratio_middle)
    View profitLineratioMiddle;

    private Context context;
    private String[] ratioStrs;
    private int[] ratioInt;

    public ProfitLineRatiosView(Context context) {
        this(context, null);
    }

    public ProfitLineRatiosView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.profit_line_ratios, this);
        ButterKnife.bind(this);
        ratioWidth = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(60);

        profitLineratioBubblelayout.setVisibility(View.INVISIBLE);
    }

    private int ratioWidth;
    private int ratioSize;
    private RelativeLayout.LayoutParams layoutParamsRatio, layoutParamsBubble;
    private LinearLayout.LayoutParams leftParams, middleParams, rightParams;

    public void setData(String[] ratioStrs, int[] ratioInt) {
        this.ratioStrs = ratioStrs;
        this.ratioInt = ratioInt;
        leftParams = (LinearLayout.LayoutParams) profitLineratioLeft.getLayoutParams();
        middleParams = (LinearLayout.LayoutParams) profitLineratioMiddle.getLayoutParams();
        rightParams = (LinearLayout.LayoutParams) profitLineratioRight.getLayoutParams();
        layoutParamsRatio = (RelativeLayout.LayoutParams) profitLineratioRatio.getLayoutParams();
        layoutParamsBubble = (RelativeLayout.LayoutParams) profitLineratioBubble.getLayoutParams();

        leftParams.weight=ratioInt[0];
        middleParams.weight=ratioInt[1];
        rightParams.weight=ratioInt[2];


        ratioSize = 0;
        for (int index : ratioInt) {
            if (index != 0) ++ratioSize;
        }

        if (ratioSize == 1){
            profitLineratioRatio.setText(ratioInt[0]>0?ratioStrs[0]:ratioInt[1]>0?ratioStrs[1]:ratioStrs[2]);

            layoutParamsRatio.addRule(ratioInt[0]>0?ALIGN_PARENT_LEFT:ALIGN_PARENT_RIGHT);
            layoutParamsBubble.addRule(ratioInt[0]>0?ALIGN_PARENT_LEFT:ALIGN_PARENT_RIGHT);
            if (ratioInt[0]>0) {
                layoutParamsBubble.leftMargin = ratioWidth / 2 + ScreenUtils.dp2px(5);
                layoutParamsRatio.leftMargin = ratioWidth / 2 - ScreenUtils.dp2px(5);
            }else {
                layoutParamsBubble.rightMargin = ratioWidth / 2 + ScreenUtils.dp2px(5);
                layoutParamsRatio.rightMargin = ratioWidth / 2 - ScreenUtils.dp2px(5);
            }

            profitLineratioLeft.setLayoutParams(leftParams);
            profitLineratioMiddle.setLayoutParams(middleParams);
            profitLineratioRight.setLayoutParams(rightParams);
//            invalidate();
        }else{
            switchRatio(ratioInt[0]>0?0:ratioInt[1]>0?1:2);
        }
        profitLineratioBubblelayout.setVisibility(View.VISIBLE);
    }




    @OnClick({R.id.profit_lineratio_left, R.id.profit_lineratio_middle, R.id.profit_lineratio_right})
    public void onViewClicked(View view) {
        if (ratioSize == 1) return;
        switch (view.getId()) {
            case R.id.profit_lineratio_left:
                switchRatio(0);
                break;

            case R.id.profit_lineratio_middle:
                switchRatio(1);
                break;

            case R.id.profit_lineratio_right:
                switchRatio(2);
                break;
        }
    }

    private void resetRatioBg(){
        leftParams.height = ScreenUtils.dp2px(10);
        middleParams.height = ScreenUtils.dp2px(10);
        rightParams.height = ScreenUtils.dp2px(10);

        profitLineratioLeft.setLayoutParams(leftParams);
        profitLineratioMiddle.setLayoutParams(middleParams);
        profitLineratioRight.setLayoutParams(rightParams);

        if (ratioSize == 2){
            if (ratioInt[0] == 0){
                shapeColor(profitLineratioMiddle,Color.parseColor("#A25DFF"),1);
                shapeColor(profitLineratioRight,Color.parseColor("#FFB43B"),2);
            }else if (ratioInt[1] == 0){
                shapeColor(profitLineratioLeft,Color.parseColor("#FF1A4A"),1);
                shapeColor(profitLineratioRight,Color.parseColor("#FFB43B"),2);
            }else {
                shapeColor(profitLineratioLeft,Color.parseColor("#FF1A4A"),1);
                shapeColor(profitLineratioMiddle,Color.parseColor("#A25DFF"),2);
            }
        }else {
            shapeColor(profitLineratioLeft,Color.parseColor("#FF1A4A"),1);
            profitLineratioMiddle.setBackgroundColor(Color.parseColor("#A25DFF"));
            shapeColor(profitLineratioRight,Color.parseColor("#FFB43B"),2);
        }
    }

    private void switchRatio(int clickSite) {
        resetRatioBg();
        layoutParamsBubble.leftMargin = 0;
        layoutParamsRatio.leftMargin = 0;
        layoutParamsBubble.rightMargin = 0;
        layoutParamsRatio.rightMargin = 0;
        layoutParamsRatio.getRules()[ALIGN_PARENT_LEFT] = 0;
        layoutParamsBubble.getRules()[ALIGN_PARENT_LEFT] = 0;
        layoutParamsRatio.getRules()[ALIGN_PARENT_RIGHT] = 0;
        layoutParamsBubble.getRules()[ALIGN_PARENT_RIGHT] = 0;

        leftParams.height = ScreenUtils.dp2px(10);
        middleParams.height = ScreenUtils.dp2px(10);
        rightParams.height = ScreenUtils.dp2px(10);

        profitLineratioRatio.setText(ratioStrs[clickSite]);

        switch (clickSite){
            case 0:
                leftParams.height = ScreenUtils.dp2px(11);
                shapeColor(profitLineratioLeft, Color.parseColor("#FF1A4A"),1);
                layoutParamsRatio.addRule(ALIGN_PARENT_LEFT);
                layoutParamsBubble.addRule(ALIGN_PARENT_LEFT);
                layoutParamsBubble.leftMargin = ratioWidth * ratioInt[0] / 200 + ScreenUtils.dp2px(5);
                layoutParamsRatio.leftMargin = ratioWidth * ratioInt[0] / 200 - ScreenUtils.dp2px(5);

                break;
            case 1:
                middleParams.height = ScreenUtils.dp2px(11);
                if (ratioSize == 2) {
                    shapeColor(profitLineratioMiddle, Color.parseColor("#A25DFF"), ratioInt[0]>0?2:1);
                }else {
                    profitLineratioMiddle.setBackgroundColor(Color.parseColor("#A25DFF"));
                }
                if (ratioInt[0]<ratioInt[2]) {
                    layoutParamsRatio.addRule(ALIGN_PARENT_LEFT);
                    layoutParamsBubble.addRule(ALIGN_PARENT_LEFT);
                    layoutParamsBubble.leftMargin = ratioWidth * ratioInt[0]/100+(ratioWidth * ratioInt[1] / 200) + ScreenUtils.dp2px(5);
                    layoutParamsRatio.leftMargin = ratioWidth * ratioInt[0]/100+(ratioWidth * ratioInt[1] / 200) - ScreenUtils.dp2px(5);
                }else {
                    layoutParamsRatio.addRule(ALIGN_PARENT_RIGHT);
                    layoutParamsBubble.addRule(ALIGN_PARENT_RIGHT);
                    layoutParamsBubble.rightMargin = ratioWidth * ratioInt[2]/100+(ratioWidth * ratioInt[1] / 200) + ScreenUtils.dp2px(5);
                    layoutParamsRatio.rightMargin = ratioWidth * ratioInt[2]/100+(ratioWidth * ratioInt[1] / 200) - ScreenUtils.dp2px(5);
                }

                break;
            case 2:
                rightParams.height = ScreenUtils.dp2px(11);
                shapeColor(profitLineratioRight, Color.parseColor("#FFB43B"), 2);
                layoutParamsRatio.addRule(ALIGN_PARENT_RIGHT);
                layoutParamsBubble.addRule(ALIGN_PARENT_RIGHT);
                layoutParamsBubble.rightMargin = ratioWidth * ratioInt[2] / 200 + ScreenUtils.dp2px(5);
                layoutParamsRatio.rightMargin = ratioWidth * ratioInt[2] / 200 - ScreenUtils.dp2px(5);
                break;
        }
        profitLineratioLeft.setLayoutParams(leftParams);
        profitLineratioMiddle.setLayoutParams(middleParams);
        profitLineratioRight.setLayoutParams(rightParams);

        profitLineratioBubblelayout.setVisibility(View.VISIBLE);
        invalidate();
    }

    private void shapeColor(View view, int color, int type){
        GradientDrawable drawable = new GradientDrawable();
        if (type == 0) {
            drawable.setCornerRadius(777);
        }else if (type == 1){
            drawable.setCornerRadii(new float[]{777,777,0,0,0,0,777,777});
        }else {
            drawable.setCornerRadii(new float[]{0,0,777,777,777,777,0,0});
        }
        drawable.setColor(color);
        view.setBackground(drawable);
    }
}
